package cn.gok.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author shikundai
 * @date 2021/3/31
 * @time 17:50
 * @description:
 */
@Component
public class SessionDaoConfig extends EnterpriseCacheSessionDAO {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    protected Serializable doCreate(Session session) {
        //获得sessionID
        Serializable sessionId=super.doCreate(session);
       /*
       * 把session对象存入redis缓存
       * */
        redisTemplate.opsForValue().set(sessionId,session);
        System.out.println("创建Session");
//        return sessionId;
        return "abcdexaa-d7a9-476d-9f4d-30892f9d00c3";
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        System.out.println("读取session:"+sessionId);
        Object obj=redisTemplate.opsForValue().get(sessionId);
//        Session session=super.doReadSession(sessionId);
        Session session= (Session) obj;
        return session;
    }

    @Override
    protected void doUpdate(Session session) {
        System.out.println("更新session:"+session);
        redisTemplate.opsForValue().set(session.getId(),session);

    }

    @Override
    protected void doDelete(Session session) {
        System.out.println("删除session");
        super.doDelete(session);
    }
}
